#include<cstdio>
#include<cstring>
#define N (1<<8)
#define FFC(i,a,b) for(int i=a;i<=b;++i)
	
int T,n,dp[2][23][N][N],g[N],num[N],f1[N][N],f2[N][N],cur,ans;
 
void init(){//预处理不可放的位置，和每一个压缩状态的骑士数目
	FFC(i,0,N-1){
		FFC(j,0,7)if(i&(1<<j))num[i]++;
		FFC(j,0,N-1){
			if(((i>>2)&j)||((j>>2)&i))f1[i][j]=1;
			if(((i>>1)&j)||((j>>1)&i))f2[i][j]=1;
		}
	}
}
int fuck(){
	memset(dp,0,sizeof(dp));
	cur=0,ans=0,dp[0][0][0][0]=1;
	FFC(i,0,7){
		FFC(j,0,n)FFC(p,0,N-1)FFC(q,0,N-1){
			if (dp[cur][j][p][q]==0)continue;
			FFC(z,0,N-1)
				if (((z&g[i+1])!=z)||num[z]+j>n||(i>=1&&f1[q][z])||(i>=2&&f2[p][z]))continue;
				else dp[cur^1][num[z]+j][q][z]+=dp[cur][j][p][q];
		}
		memset(dp[cur],0,sizeof(dp[cur])),cur^=1;
	}
	FFC(i,0,N-1)FFC(j,0,N-1)ans+=dp[cur][n][i][j];
	return ans;
}
 
int main() {
	init();char str[N];
	scanf("%d",&T);
	while (T--){
		scanf("%d",&n);	
		memset(g,0,sizeof(g));
		FFC(i,1,8){
			scanf("%s",str);
			FFC(j,0,7){
				g[i]<<=1;
				if(str[j]=='.')g[i]|=1;
			}
		}
		printf("%d\n",fuck());
	}
	return 0;
}
